STATS 32 Session 2: Basic R objects

Kenneth Tay

Sep 26, 2019

http://web.stanford.edu/~kjytay/courses/stats32-aut2019/

Recap of session 1: R as calculator

3 + 4
## [1] 7
exp(1)
## [1] 2.718282
log(exp(1))
## [1] 1
pi
## [1] 3.141593
5.2 / 0
## [1] Inf

Recap of session 1: Variables

x <- 3
x <- 3
x <- 3
y <- "abc"
x <- 3
y <- "abc"
x <- 3
y <- "abc"
y <- 5
x <- 3
y <- "abc"
y <- 5

Reminder!

Agenda for today

Vectors

vec <- c("a", "b", "c")
vec
## [1] "a" "b" "c"

Vectors

vec <- 1:100
vec
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
##  [18]  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34
##  [35]  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51
##  [52]  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68
##  [69]  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85
##  [86]  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100

Vectors

even <- 1:50 * 2
even
##  [1]   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34
## [18]  36  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68
## [35]  70  72  74  76  78  80  82  84  86  88  90  92  94  96  98 100

Vectors

even <- 1:50 * 2
even
##  [1]   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34
## [18]  36  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68
## [35]  70  72  74  76  78  80  82  84  86  88  90  92  94  96  98 100

How can we get the odd numbers from 1 to 100 from even?

Vectors

How can we get the odd numbers from 1 to 100 from even?

odd <- even - 1
odd
##  [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45
## [24] 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91
## [47] 93 95 97 99

Vectors

We can use the c() function to concatenate vectors.

z <- 1:5
z <- c(z, 3, z)
z
##  [1] 1 2 3 4 5 3 1 2 3 4 5

Vectors: Indexing

To extract a subset of elements by their indices, put a vector of indices in square brackets

even
##  [1]   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34
## [18]  36  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68
## [35]  70  72  74  76  78  80  82  84  86  88  90  92  94  96  98 100
even[1]
## [1] 2

Vectors: Indexing

To extract a subset of elements by their indices, put a vector of indices in square brackets

even
##  [1]   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34
## [18]  36  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68
## [35]  70  72  74  76  78  80  82  84  86  88  90  92  94  96  98 100
even[3:7]
## [1]  6  8 10 12 14

Vectors: Indexing

To extract a subset of elements by their indices, put a vector of indices in square brackets

even
##  [1]   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34
## [18]  36  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68
## [35]  70  72  74  76  78  80  82  84  86  88  90  92  94  96  98 100
even[c(3,5)]
## [1]  6 10

Note: even[3,5] will not work!

Vectors: Negative indexing

To extract all except a few indices, put a negative sign before the vector of indices

even
##  [1]   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34
## [18]  36  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68
## [35]  70  72  74  76  78  80  82  84  86  88  90  92  94  96  98 100
even[-c(1,2)]
##  [1]   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38
## [18]  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72
## [35]  74  76  78  80  82  84  86  88  90  92  94  96  98 100

Vectors: Length

Use the length function to figure out how many elements there are in a vector

even
##  [1]   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34
## [18]  36  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68
## [35]  70  72  74  76  78  80  82  84  86  88  90  92  94  96  98 100
length(even)
## [1] 50

Vectors: Things to watch out for

What happens if we try to extract an invalid index?

odd[0]

Vectors: Things to watch out for

What happens if we try to extract an invalid index?

odd[0]
## numeric(0)

No error thrown!!

Vectors: Things to watch out for

What happens if we try to extract an invalid index?

odd[0]
## numeric(0)

No error thrown!!

odd[51]

Vectors: Things to watch out for

What happens if we try to extract an invalid index?

odd[0]
## numeric(0)

No error thrown!!

odd[51]
## [1] NA

No error thrown!!

Vectors: Things to watch out for

If we try to assign a vector with different types, type coercion happens.

c(1, 2, "a")
## [1] "1" "2" "a"

Matrices and arrays

Two-dimensional analogs of vectors

A <- matrix(1:12, nrow = 3)
A
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12

Indexing: put the rows you want before the comma, columns you want after the comma

A[1, 2]
## [1] 4

Matrices: Indexing example

A
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12

What does A[c(1,3), c(2,4)] return?

Matrices: Indexing example

A
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12

What does A[c(1,3), c(2,4)] return?

A[c(1,3), c(2,4)]
##      [,1] [,2]
## [1,]    4   10
## [2,]    6   12

Lists

List example

cars <- list(make = "Honda", 
             models = c("Fit", "CR-V", "Odyssey"), 
             available = c(TRUE, TRUE, TRUE))

Extracting parts of a list

Use [[ or $ notation to refer to a specific key-value pair

cars$make         # no quotation marks
## [1] "Honda"
cars[["models"]]  # remember quotation marks!
## [1] "Fit"     "CR-V"    "Odyssey"

What is a data frame?

Example of a dataset

Data frame/tibble printed to screen

df
##   votes_dem votes_gop
## 1    486351     91189
## 2       318       211
## 3      5904     10239
tbl
## # A tibble: 3 x 2
##   votes_dem votes_gop
##       <dbl>     <dbl>
## 1    486351     91189
## 2       318       211
## 3      5904     10239

Structure of data frame

str(df)
## 'data.frame':    3 obs. of  2 variables:
##  $ votes_dem: num  486351 318 5904
##  $ votes_gop: num  91189 211 10239

Data frames “under the hood”

Data frames “under the hood”

is.list(df)
## [1] TRUE
df$votes_dem
## [1] 486351    318   5904

Today’s dataset: Fuel economy

(Source: SuperCars)

fueleconomy: Package information on CRAN

https://cran.r-project.org/web/packages/fueleconomy/index.html









Optional material

Measures of central tendency

Measures of spread